package cn.iocoder.yudao.module.system.service.merchant;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.system.controller.admin.merchant.vo.AdminGetMerchantRespVO;
import cn.iocoder.yudao.module.system.controller.admin.merchant.vo.MerchantReqVO;
import cn.iocoder.yudao.module.system.controller.admin.merchant.vo.MerchantSaveReqVO;
import cn.iocoder.yudao.module.system.convert.merchant.MerchantConvert;
import cn.iocoder.yudao.module.system.dal.dataobject.merchant.MerchantDO;
import com.google.common.annotations.VisibleForTesting;
import org.springframework.stereotype.Service;
import cn.iocoder.yudao.module.system.dal.mysql.merchant.*;

import javax.annotation.Resource;

import java.util.Collections;
import java.util.Objects;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.USER_NOT_EXISTS;

@Service
public class MerchantUserListServiceImpl implements MerchantUserListService{

    @Resource
    private MerchantMapper merchantMapper;

    @Override
    public PageResult<MerchantDO> getMerchantList(MerchantReqVO reqVO) {
        return merchantMapper.selectPage(reqVO);
    }

    @Override
    public void updateMerchantStatus(long id, Integer status) {
        // 校验用户存在
        validateUserExists(id);
        // 更新状态
        MerchantDO updateObj = new MerchantDO();
        updateObj.setMerchantId(id);
        updateObj.setStatus(status);
        merchantMapper.updateById(updateObj);
    }

    @Override
    public void updateMerchant(MerchantSaveReqVO updateReqVO) {
        validateUserExists(updateReqVO.getId());
        MerchantDO updateObj =new MerchantDO();
        updateObj.setMerchantId(updateReqVO.getId());
        updateObj.setMerchantName(updateReqVO.getMerchantName());
        updateObj.setContactPhone(updateReqVO.getContactPhone());
        updateObj.setContactEmail(updateReqVO.getContactEmail());
        updateObj.setContactAddress(updateReqVO.getContactAddress());
        updateObj.setMerchantIdentity(updateReqVO.getMerchantIdentity());
        updateObj.setWebUrl(String.join(",",updateReqVO.getWebUrl()));
        updateObj.setDescription(updateReqVO.getDescription());
        updateObj.setLegalRepresentative(updateReqVO.getLegalRepresentative());
        merchantMapper.updateById(updateObj);

    }

    @Override
    public void deleteMerchant(Long id) {
        MerchantDO user = validateUserExists(id);

        merchantMapper.deleteByUserId(user);

    }

    @Override
    public AdminGetMerchantRespVO getMerchant(Long id) {
        MerchantConvert convert = new MerchantConvert();
        MerchantDO user = merchantMapper.selectByUserId(id);
        if(Objects.isNull(user)){
            return null;
        }
        return convert.convertList((Collections.singletonList(user))).get(0);
    }

    @VisibleForTesting
    MerchantDO  validateUserExists(Long id) {
        if (id == null) {
            return null;
        }
        MerchantDO user = merchantMapper.selectByUserId(id);
        if (user == null) {
            throw exception(USER_NOT_EXISTS);
        }
        return user;
    }

    @Override
    public Boolean validateMerchantExist(Long merchantId) {
        return merchantMapper.selectByUserId(merchantId) != null;
    }
}
